# Reference: <https://u-boot.readthedocs.io/en/latest/board/amlogic>
# Maintainer: Ferass El Hafidi <funderscore@postmarketos.org>
# Co-Maintainer: exkc <exxxxkc@getgoogleoff.me>
# Co-Maintainer: hexaheximal <hexaheximal@proton.me>
pkgname=u-boot-amlogic
pkgver=2024.04
pkgrel=0
pkgdesc="U-Boot bootloader for Amlogic AArch64-based devices"
url="https://u-boot.org/"
arch="aarch64"
license="GPL-2.0-or-later OFL-1.1 BSD-2-Clause BSD-3-Clause eCos-2.0 IBM-pibs
	ISC LGPL-2.0-only LGPL-2.1-only X11 proprietary"
makedepends="$depends_dev
	bc
	bison
	crust
	dtc
	flex
	openssl-dev
	py3-setuptools
	python3-dev
	qemu-x86_64
	swig
	bash
"
options="!check"
# https://u-boot.readthedocs.io/en/latest/board/amlogic/pre-generated-fip.html
_amlogicblob_commit="8599bc77b17f38e69275f6145acc5792faab735e"
source="
	https://source.denx.de/u-boot/u-boot/-/archive/v$pkgver/u-boot-v$pkgver.tar.gz
	0001-add-x96-mini-support.patch
	amlogic-fip.tar.gz::https://github.com/LibreELEC/amlogic-boot-fip/archive/$_amlogicblob_commit.tar.gz
"
builddir="$srcdir/u-boot-v$pkgver"
provides="u-boot=$pkgver-r$pkgrel"
replaces="
	u-boot-amediatech-x96-mini
	u-boot-bananapi-bpi-cm4
	u-boot-beelink-gt1
	u-boot-librecomputer-lepotato
	u-boot-odroid-c4
	u-boot-odroid-n2plus
	u-boot-radxa-zero
"
_fip_builddir="$srcdir/amlogic-boot-fip-$_amlogicblob_commit"

# Format: <board>:<board u-boot defconfig>,<amlogic-boot-fip board files>
_boards="
	amediatech-x96-mini:x96_mini,jethub-j80
	bananapi-bpi-cm4-cm4io:bananapi-cm4-cm4io,bananapi-cm4io
	beelink-gt1:beelink-gt1-ultimate,beelink-gt1
	librecomputer-lepotato:libretech-cc,lepotato
	odroid-c4:odroid-c4,odroid-c4
	odroid-n2plus:odroid-n2,odroid-n2-plus
	radxa-zero:radxa-zero,radxa-zero
"

for board in $_boards; do
	_board_codename=${board%%:*}
	subpackages="$subpackages $pkgname-$_board_codename:${_board_codename//-/_}"
done

prepare() {
	default_prepare
}

build() {
	for board in $_boards; do
		local board_names=${board##*:} board_config board_codename="" board_fip_dir
		board_config=${board_names%%,*}
		board_codename=${board%%:*}
		board_fip_dir=${board##*,}

		msg "Building U-Boot for $board_codename (config: $board_config, FIP: $board_fip_dir)"
		touch include/config.h
		LC_ALL=C date +'#define U_BOOT_DATE "%b %d %C%y"' > include/timestamp_autogenerated.h
		LC_ALL=C date +'#define U_BOOT_TIME "%T"' >> include/timestamp_autogenerated.h
		make -j8 ${board_config}_defconfig all

		msg "Building Amlogic firmware package for $board_codename (FIP: $board_fip_dir)"
		cd $_fip_builddir
		mkdir -p ./build
		./build-fip.sh $board_fip_dir $builddir/u-boot.bin ./build

		cd ./build
		dd if=u-boot.bin.sd.bin of=u-boot-$board_codename.bin.sd-stripped.bin conv=fsync,notrunc bs=512 skip=1
		cd $builddir
	done
}

package() {
	mkdir -p "$pkgdir"
}

_board_package() {
	pkgdesc="U-Boot bootloader for Amlogic AArch64-based devices ($1)"
	depends="u-boot-amlogic"
	board_codename=$1
	board_fip_dir=$2
	mkdir -p "$subpkgdir/usr/share/u-boot/$board_codename/"
	install -D -m644 "$srcdir/amlogic-boot-fip-$_amlogicblob_commit/build/u-boot-$board_codename.bin.sd-stripped.bin" \
		"$subpkgdir/usr/share/u-boot/$board_codename/u-boot.bin.sd-stripped.bin"
}

for board in $_boards; do
	_board_codename=${board%%:*}
	_board_fip_dir=${board##*,}
	eval "${_board_codename//-/_}() { _board_package $_board_codename $_board_fip_dir; }"
done

sha512sums="
be9b0176337fe9321ac244a16a17dfc1fb976b019de36c78d52149fe7c9d89d2907321be4c0c2faf0d47a4982212501f29063dbd01a005ce2c54e072159e0e3e  u-boot-v2024.04.tar.gz
415e81dedca92da59bb12025e64a02b14c8cd43c1a7d80b58e808db8f61402ecf396bf6c072c1cc18546254a5c88c9573c255c37496078a71c825abea87211be  0001-add-x96-mini-support.patch
74c57daff859e7f449bd2743c7a8c0358b8dbe5d321ddb3a5b242b4d35683890d500d23e769490b1419a18bed233aea03ee11103bb4d646c502e4018283ba975  amlogic-fip.tar.gz
"
